<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class shop extends core {

	public function __construct() {
		parent::__construct();
	}
	
	//我是商家首页
	public function index() {
		if ($this->member_id<=0) error('请登录', -100);
		if ($this->shop_id<=0) error('您不是商家');
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		//店铺
		$sql = "SELECT * FROM {$this->tbp}shop WHERE id='{$this->shop_id}'";
		$shop = $this->db->get_row($sql);
		if ($shop) {
			$sql = "SELECT id, name, pic, price FROM {$this->tbp}goods 
				WHERE status='1' AND shop_id='{$this->shop_id}' ORDER BY sort ASC, id DESC LIMIT {$offset}, {$pagesize}";
			$goods = $this->db->get_results($sql);
			$shop->goods = $goods;
			$shop = add_domain_deep($shop, array("avatar", "poster_pic", "pic"));
		}
		//订单总数
		$sql = "SELECT count(*) FROM {$this->tbp}order WHERE shop_id='{$this->shop_id}'";
		$order_total = intval($this->db->get_var($sql));
		//本月订单总数
		$sql = "SELECT count(*) FROM {$this->tbp}order
			WHERE shop_id='{$this->shop_id}' AND status>0 AND add_time>='".strtotime(date('Y-m-1'))."' AND add_time<='".strtotime(date('Y-m-d 23:59:59'))."'";
		$order_month_total = intval($this->db->get_var($sql));
		//我的收入
		$sql = "SELECT commission FROM {$this->tbp}member WHERE id='{$this->member_id}'";
		$commission = floatval($this->db->get_var($sql));
		
		success(array('order_total'=>$order_total, 'order_month_total'=>$order_month_total, 'commission'=>$commission, 'shop'=>$shop));
	}
	
	//店铺订单列表，0未支付，1已支付，2已发货，3完成（已收货），-1取消，-2退款，-3退货
	public function order() {
		if ($this->member_id<=0) error('请登录', -100);
		if ($this->shop_id<=0) error('您不是商家');
		$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : '';
		$status = isset($_GET['status']) ? $_GET['status'] : '';
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$where = " o.shop_id='{$this->shop_id}'";
		if (strlen($status)) {
			if ($status=='-2,-3') {
				$where .= " AND (o.status IN ({$status}) OR o.ask_refund_time>0)";
			} else {
				$where .= " AND o.status IN ({$status})";
			}
		}
		if ($keyword) {
			$where .= " AND (o.order_sn LIKE '%{$keyword}%' OR o.name LIKE '%{$keyword}%' OR o.mobile LIKE '%{$keyword}%')";
		}
		$sql = "SELECT o.* FROM {$this->tbp}order o
			WHERE {$where} ORDER BY o.id DESC LIMIT {$offset},{$pagesize}";
		//echo $sql;
		$orders = $this->db->get_results($sql);
		if ($orders) {
			foreach ($orders as $k=>$o) {
				$sql = "SELECT * FROM {$this->tbp}order_goods WHERE order_id='{$o->id}'";
				$goods = $this->db->get_results($sql);
				$goods = add_domain_deep($goods, array("goods_pic"));
				$orders[$k]->goods = $goods;
			}
		}
		success($orders);
	}

	//订单详情
	public function order_detail() {
		$member = c('member');
		$member->order_detail(true);
	}
	
	//店铺列表
	public function shop_list() {
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$sql = "SELECT *
			FROM {$this->tbp}shop
			WHERE status='1' ORDER BY sort ASC, id DESC
			LIMIT {$offset}, {$pagesize}";
		$shops = $this->db->get_results($sql);
		if ($shops) {
			foreach ($shops as $key => $shop) {
				//$shops[$key]->comment_time = date("Y-m-d H:i:s", $shop->comment_time);
			}
			$shops = add_domain_deep($shops, array("avatar", "poster_pic"));
		}
		success($shops);
	}

	//店铺详情
	public function detail() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$sql = "UPDATE {$this->tbp}shop SET clicks=clicks+1 WHERE id='{$id}'";
		$this->db->query($sql);
		$sql = "SELECT * FROM {$this->tbp}shop WHERE id='{$id}' AND status='1'";
		//echo $sql;
		$shop = $this->db->get_row($sql);
		if ($shop) {
			$sql = "SELECT * FROM {$this->tbp}ad WHERE status='1' AND shop_id='{$id}' ORDER BY sort ASC, id DESC LIMIT 5";
			$ads = $this->db->get_results($sql);
			$shop->ads = $ads;
			$sql = "SELECT * FROM {$this->tbp}goods 
				WHERE status='1' AND shop_id='{$id}' ORDER BY sort ASC, id DESC LIMIT {$offset}, {$pagesize}";
			$goods = $this->db->get_results($sql);
			if ($goods) {
				foreach ($goods as $k => $g) {
					unset($goods[$k]->content);
					$goods[$k]->price = $this->goods_mod->get_min_price(array($g->price, $g->promote_price));
				}
			}
			$shop->goods = $goods;
			$shop = add_domain_deep($shop, array("avatar", "poster_pic", "pic"));
		}
		success($shop);
	}
	
	//申请入驻
	public function apply() {
		if ($this->member_id<=0) error('请先登录', -100);
		$sql = "SELECT count(*) FROM {$this->tbp}shop_apply WHERE member_id='{$this->member_id}'";
		if (intval($this->db->get_var($sql))) error('您已经申请了入驻，请勿重复提交');
		if (IS_POST) {
			$type_id =  isset($_POST['type_id']) ? intval($_POST['type_id']) : 0;
			$type_sub =  isset($_POST['type_sub']) ? intval($_POST['type_sub']) : 0;
			$name = isset($_POST['name']) ? trim($_POST['name']) : '';
			$idcard_pic1 = isset($_POST['idcard_pic1']) ? trim($_POST['idcard_pic1']) : '';
			$idcard_pic2 = isset($_POST['idcard_pic2']) ? trim($_POST['idcard_pic2']) : '';
			$business_license_pic = isset($_POST['business_license_pic']) ? trim($_POST['business_license_pic']) : '';
			$license_pic = isset($_POST['license_pic']) ? trim($_POST['license_pic']) : '';
			$other_pic = isset($_POST['other_pic']) ? trim($_POST['other_pic']) : '';
			$contacter = isset($_POST['contacter']) ? trim($_POST['contacter']) : '';
			$mobile = isset($_POST['mobile']) ? trim($_POST['mobile']) : '';
			$province = isset($_POST['province']) ? trim($_POST['province']) : '';
			$city = isset($_POST['city']) ? trim($_POST['city']) : '';
			$district = isset($_POST['district']) ? trim($_POST['district']) : '';
			$address = isset($_POST['address']) ? trim($_POST['address']) : '';
			$reason = isset($_POST['reason']) ? trim($_POST['reason']) : '';
	
			//店铺名称长度2~24个字符 willson2016/2/3
			$len = mb_strlen($name);
			if ($len<2 || $len>24) error('名称不能超过24个字符');
			
			//店铺名称是否存在
			$sql = "SELECT count(*) FROM {$this->tbp}shop WHERE name='{$name}'";
			if (intval($this->db->get_var($sql))) error('该名称已经存在');
			
			if ($type_sub>0) $type_id = 5; //团餐
			$sql = "INSERT INTO {$this->tbp}shop_apply (member_id, name, province, city, district, address, contacter, mobile, reason, 
				type_id, type_sub, idcard_pic1, idcard_pic2, business_license_pic, license_pic, other_pic, status, add_time) 
				VALUES('{$this->member_id}', '{$name}', '{$province}', '{$city}', '{$district}', '{$address}', '{$contacter}', '{$mobile}', '{$reason}', 
				'{$type_id}', '{$type_sub}', '{$idcard_pic1}', '{$idcard_pic2}', '{$business_license_pic}', '{$license_pic}', '{$other_pic}', '0', '".time()."')";
			$this->db->query($sql);
		}
		success('ok');
	}

	//编辑店铺
	public function edit_info() {
		if (isset($_POST['name']) && trim($_POST['name'])) {
			$name = trim($_POST['name']);
			$sql = "SELECT count(*) FROM {$this->tbp}shop WHERE name='{$name}' AND id!='{$this->shop_id}'";
			$exists = intval($this->db->get_var($sql));
			if (intval($this->db->get_var($sql)) > 0) error('该名称已经存在');
		}
		$fields = array("name", "description", "qq", "weixin", "email", "avatar", "poster_pic", 
			"return_province", "return_city", "return_district", "return_address", "return_name", "return_mobile", 
			"province", "city", "district", "address", "contacter", "mobile"
		);
		$set_sql = '';
		foreach ($fields as $field) {
			if (isset($_POST[$field])) {
				$value = trim($_POST[$field]);
				if ($set_sql) {
					$set_sql .= ", `{$field}`='{$value}'";
				} else {
					$set_sql = "`{$field}`='{$value}'";
				}
			}
		}
		if ($set_sql) {
			$sql = "UPDATE {$this->tbp}shop SET {$set_sql} WHERE id='{$this->shop_id}'";	
			$this->db->query($sql);
		}
		$sql = "SELECT * FROM {$this->tbp}shop WHERE id='{$this->shop_id}'";
		$shop = $this->db->get_row($sql);
		success($shop);
	}

	//门店地图
	public function map() {
		$this->detail();
	}
}
